<?php

namespace App\Models\Common;

use App\Models\Traits\HasOperateLog;
use App\Models\Traits\HasTree;
use App\Models\User\User;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class DataSource extends Model
{
	use HasDateTimeFormatter;
    use SoftDeletes;
    use HasOperateLog;
    use HasTree;

    protected $table = 'data_sources';

    protected $guarded = [];

    protected $appends = ['show_name'];

    public function showName() : Attribute
    {
        return Attribute::make(
            get: fn () => ($this->aliasname ? $this->aliasname . ' - ' : '') . $this->name,
        );
    }

    public function creator() : BelongsTo
    {
        return $this->belongsTo(User::class, 'creator_id');
    }

    public function parent() : BelongsTo
    {
        return $this->belongsTo(self::class, 'parent_id');
    }

    public function children(): HasMany
    {
        return $this->hasMany(self::class, 'parent_id');
    }
}
